﻿#include "HardwareInfo.h"
#include <cctype>
#include <string>
#include <atomic>
#include <ctime>

#if (defined(WIN32) || defined(WIN64))
#include <windows.h>
#include <intrin.h>
#include <array>
#include <iphlpapi.h>

#else
#include <fstream>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

#endif

#include "../../common/tinyformat.h"


std::string	NewObjectId(uint32_t nextValue) {
    static  std::atomic<uint32_t> s_nextInc = 0 ; //缓存计数
    static std::string machineCode32 = "A314"; //缓存机器码数据
    static uint16_t pid_cache = 0;  //当前进程PID
    uint32_t objectCID = 1;

    if (nextValue == 0xFFFFFFFF) {
        objectCID = s_nextInc.fetch_add(1);
    }else{
        objectCID = nextValue; 
    }

    if (pid_cache == 0) {
        pid_cache = static_cast<uint16_t>(getCurrentProcessId());
    }

    uint32_t timestamp32 = static_cast<uint32_t>(time(NULL));
  /*|MachineCode(4字节)|PID(4字节)|时间戳(8字节)|Count(8字节)|*/
    std::string code = tfm::format("%s%04X%08X%08X",  machineCode32 , pid_cache ,  timestamp32, objectCID);
    return code; 
}


uint32_t getCurrentProcessId() {
#if (defined(WIN32) || defined(WIN64))
    DWORD pid = GetCurrentProcessId(); 
    return static_cast<uint32_t>(pid); 
#else
    pid_t pid = getpid();
    return static_cast<uint32_t>(pid);
#endif

}